﻿#region 信息

// Copyright©2014 Rex Rao all rights reserved.
// File name : SoMcCoffee\Sohobloo.SoMcCoffee\EntityConfigurationBase.cs
// Author : Sohobloo
// Create time : 2014/01/31 23:40
// Update time : 2014/02/05 8:57

#endregion

#region 引用

using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;

#endregion

namespace Sohobloo.SoMcCoffee.Entity
{
    public abstract class EntityConfigurationBase<TEntity> : EntityTypeConfiguration<TEntity>, IEntityMapper
        where TEntity : EntityBase
    {
        #region 属性

        public virtual string TablePrefix
        {
            get { return string.Empty; }
        }

        public virtual string TableName
        {
            get { return typeof(TEntity).Name; }
        }

        #endregion 属性

        #region 构造

        protected EntityConfigurationBase()
        {
            Configurate();
        }

        #endregion 构造

        #region 方法

        public void RegistTo(ConfigurationRegistrar configurationRegistrar)
        {
            configurationRegistrar.Add(this);
        }

        protected virtual void ConfigurateExtension()
        {
        }

        private void Configurate()
        {
            ToTable(string.Concat(TablePrefix, TableName));
            HasKey(entity => entity.Id);
            Property(entity => entity.CreateUser).IsRequired().HasMaxLength(50);
            Property(entity => entity.CreateTime).IsRequired();
            Property(entity => entity.UpdateUser).IsRequired().HasMaxLength(50);
            Property(entity => entity.UpdateTime).IsRequired();
            Property(entity => entity.Timestamp).IsRequired().IsRowVersion();
            ConfigurateExtension();
        }

        #endregion 方法
    }
}